import { Component, OnInit, OnDestroy, ChangeDetectorRef, Inject, forwardRef } from '@angular/core';
import { path } from '../../../../path.config';
import { AppComponent } from '../../../../app.component';

import { masterService } from '../../../../../lib/master.service';
import { saveVarService } from '../../../../../lib/saveVar.service';
import { NzModalService } from 'ng-zorro-antd';
@Component({
    selector: 'exchange-card-template-grid',
    templateUrl: './exchange-card-template-grid.component.html',
    styleUrls: ['./exchange-card-template-grid.component.css'],
    providers: [forwardRef(() => AppComponent)]
})
export class ExchangeCardTemplateGridComponent implements OnInit {
    // api
    // 表格数据
    public getList: any = path.cardManagement.cardTemplate.getList;
    // 列表项详细数据
    public getListItem: any = path.cardManagement.cardTemplate.getListItem;
    // 列表项数组
    public rows: Array<any> = [];
    // 表格数据头部标题
    public columns: Array<any> = [
        { title: '模板id'},
        { title: '有效期'},
        { title: '模板名称'},
        { title: '商品sn'},
        { title: '商品销售名'},
        { title: '兑换链接'},
        { title: '最后更新时间'},
        { title: '操作人'},
        { title: '已领数量'},
        { title: '操作'},
    ];
    // 当前页码
    public page: any = 1;
    // 一共多少页
    public itemsPerPage: any = 10;
    // 分页最多显示的页数
    public maxSize: number = 5;
    // 总页数计数变化时，事件数等于总页数计数
    public numPages: number = 1;
    // 总共的列表项数
    public totalItems: number = 0;
    // 分页器的文本配置
    public localText = {
        firstBtn: '首页',
        lastBtn: '末页',
        previousBtn: '上一页',
        nextBtn: '下一页'
    }

    public filter = {
        pageSize: this.itemsPerPage,
        pageIndex: this.page,
        name: '',
        goodsName: ''
    }

    constructor(public _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        public _saveVarService: saveVarService,
        private confirmServ: NzModalService,
        @Inject(forwardRef(() => AppComponent)) public app: AppComponent) { }

    ngOnInit() {
        this.onChangeTable();
        this._masterService.CardTemplateReload
            .subscribe(() => {
                this.onChangeTable();
            })
    }
    
    ngOnDestroy() {
        this.cdRef.detach();
    }

    // 渲染表格
    public onChangeTable = (urlObj?, type?) => {
        if (type == 1 && urlObj) {
            // 1为筛选
            this.filter.name = urlObj.name;
            this.filter.goodsName = urlObj.goodsName;
        } else if (urlObj) {
            this.filter.pageSize = urlObj.itemsPerPage;
            this.filter.pageIndex = urlObj.page;
        }
        this._masterService
            .getData(this.getList, this.filter)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.rows = res.data.items;
                    this.rows.forEach((rowVal, i) => {
                        // url
                        rowVal.url = vmallHost + "h5/cards/index.html";
                    })
                    this.totalItems = res.data.pagination.totalCount;
                    this.itemsPerPage = res.data.pagination.pageSize;
                    this.page = parseInt(res.data.pagination.pageIndex);
                    this.cdRef.detectChanges();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    
    // 打开编辑弹窗
    public openEditModal(rowId) {
        let urlObject = {
            id: rowId
        }
        // 获取列表详情
        this._masterService
            .getData(this.getListItem, urlObject)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this._saveVarService.cardTemplateEdit = res.data;
                    this._masterService.openCardTemplate.emit(res.data);
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }
}
